package com.ruoyi.bearing.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.bearing.domain.BearingDataset;
import com.ruoyi.bearing.mapper.BearingConditionMapper;
import com.ruoyi.bearing.domain.BearingCondition;
import com.ruoyi.bearing.service.IBearingConditionService;

/**
 * 数据集工况Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-12
 */
@Service
public class BearingConditionServiceImpl implements IBearingConditionService 
{
    @Autowired
    private BearingConditionMapper bearingConditionMapper;

    /**
     * 查询数据集工况
     * 
     * @param cid 数据集工况主键
     * @return 数据集工况
     */
    @Override
    public BearingCondition selectBearingConditionByCid(String cid)
    {
        return bearingConditionMapper.selectBearingConditionByCid(cid);
    }

    /**
     * 查询数据集工况列表
     * 
     * @param bearingCondition 数据集工况
     * @return 数据集工况
     */
    @Override
    public List<BearingCondition> selectBearingConditionList(BearingCondition bearingCondition)
    {
        return bearingConditionMapper.selectBearingConditionList(bearingCondition);
    }

    /**
     * 新增数据集工况
     * 
     * @param bearingCondition 数据集工况
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBearingCondition(BearingCondition bearingCondition)
    {
        bearingCondition.setCreateTime(DateUtils.getNowDate());
        int rows = bearingConditionMapper.insertBearingCondition(bearingCondition);
        insertBearingDataset(bearingCondition);
        return rows;
    }

    /**
     * 修改数据集工况
     * 
     * @param bearingCondition 数据集工况
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBearingCondition(BearingCondition bearingCondition)
    {
        bearingCondition.setUpdateTime(DateUtils.getNowDate());
        bearingConditionMapper.deleteBearingDatasetByDatasetId(bearingCondition.getCid());
        insertBearingDataset(bearingCondition);
        return bearingConditionMapper.updateBearingCondition(bearingCondition);
    }

    /**
     * 批量删除数据集工况
     * 
     * @param cids 需要删除的数据集工况主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBearingConditionByCids(String[] cids)
    {
        bearingConditionMapper.deleteBearingDatasetByDatasetIds(cids);
        return bearingConditionMapper.deleteBearingConditionByCids(cids);
    }

    /**
     * 删除数据集工况信息
     * 
     * @param cid 数据集工况主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBearingConditionByCid(String cid)
    {
        bearingConditionMapper.deleteBearingDatasetByDatasetId(cid);
        return bearingConditionMapper.deleteBearingConditionByCid(cid);
    }

    /**
     * 新增数据集信息
     * 
     * @param bearingCondition 数据集工况对象
     */
    public void insertBearingDataset(BearingCondition bearingCondition)
    {
        List<BearingDataset> bearingDatasetList = bearingCondition.getBearingDatasetList();
        String cid = bearingCondition.getCid();
        if (StringUtils.isNotNull(bearingDatasetList))
        {
            List<BearingDataset> list = new ArrayList<BearingDataset>();
            for (BearingDataset bearingDataset : bearingDatasetList)
            {
                bearingDataset.setDatasetId(cid);
                list.add(bearingDataset);
            }
            if (list.size() > 0)
            {
                bearingConditionMapper.batchBearingDataset(list);
            }
        }
    }
}
